package Store;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Account {
	private String accountID = "";
	private String email = "";
	private String hashPassword = "";
	private String securityQuestion1 = "";
	private String securityQuestion2 = "";
	private String securityAnswer1 =  "";
	private String securityAnswer2 = "";
	private Customer customer;
	private String customerID = "";
	private Date lastLogin = new Date();
	private Float balance = new Float(0);
	private Boolean storeUser = false;
	
	public Account(String email, String hashPassword){
		this.email = email;
		this.hashPassword = hashPassword;
		storeUser = true;
	}
	
	public String setAccountID(){
		return email;
	}
	
	public Account(String email, String hashPassword, String securityQuestion1, String securityQuestion2, String securityAnswer1, String securityAnswer2){
		this.setEmail(email);
		this.setHashPassword(hashPassword);
		this.setSecurityQuestion1(securityQuestion1);
		this.setSecurityQuestion2(securityQuestion2);
		this.setSecurityAnswer1(securityAnswer1);
		this.setSecurityAnswer2(securityAnswer2);
		this.setCustomer(customer);
		this.setAccountID(this.setAccountID());
	}
	
	public Account(String email, String hashPassword, String securityQuestion1, String securityAnswer1){
		this.setEmail(email);
		this.setHashPassword(hashPassword);
		this.setSecurityQuestion1(securityQuestion1);
		this.setSecurityAnswer1(securityAnswer1);
		this.setCustomer(customer);
		this.setAccountID(this.setAccountID());
	}
	
	public Account(String email, String hashPassword, String securityQuestion1, String securityQuestion2, String securityAnswer1, String securityAnswer2, Date lastLogin){
		this.setEmail(email);
		this.setHashPassword(hashPassword);
		this.setSecurityQuestion1(securityQuestion1);
		this.setSecurityQuestion2(securityQuestion2);
		this.setSecurityAnswer1(securityAnswer1);
		this.setSecurityAnswer2(securityAnswer2);
		this.setCustomer(customer);
		this.setLastLogin(lastLogin);
		this.setAccountID(this.setAccountID());
	}
	
	public Account(String email, String hashPassword, String securityQuestion1, String securityAnswer1, Date lastLogin){
		this.setEmail(email);
		this.setHashPassword(hashPassword);
		this.setSecurityQuestion1(securityQuestion1);
		this.setSecurityAnswer1(securityAnswer1);
		this.setCustomer(customer);
		this.setLastLogin(lastLogin);
		this.setAccountID(this.setAccountID());
	}
	
	public Account(String email, String hashPassword, String securityQuestion1, String securityQuestion2, String securityAnswer1, String securityAnswer2, Date lastLogin, float balance){
		this.setEmail(email);
		this.setHashPassword(hashPassword);
		this.setSecurityQuestion1(securityQuestion1);
		this.setSecurityQuestion2(securityQuestion2);
		this.setSecurityAnswer1(securityAnswer1);
		this.setSecurityAnswer2(securityAnswer2);
		this.setCustomer(customer);
		this.setLastLogin(lastLogin);
		this.setBalance(balance);
		this.setAccountID(this.setAccountID());
	}
	
	public Account(String email, String hashPassword, String securityQuestion1, String securityAnswer1, Date lastLogin, float balance){
		this.setEmail(email);
		this.setHashPassword(hashPassword);
		this.setSecurityQuestion1(securityQuestion1);
		this.setSecurityAnswer1(securityAnswer1);
		this.setCustomer(customer);
		this.setLastLogin(lastLogin);
		this.setBalance(balance);
		this.setAccountID(this.setAccountID());
	}
	
	public Account() {
	}

	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getHashPassword() {
		return hashPassword;
	}
	
	public void setHashPassword(String hashPassword) {
		this.hashPassword = hashPassword;
	}
	
	public String getSecurityQuestion1() {
		return securityQuestion1;
	}
	
	public void setSecurityQuestion1(String securityQuestion1) {
		this.securityQuestion1 = securityQuestion1;
	}
	
	public String getSecurityQuestion2() {
		return securityQuestion2;
	}
	
	public void setSecurityQuestion2(String securityQuestion2) {
		this.securityQuestion2 = securityQuestion2;
	}
	
	public String getSecurityAnswer1() {
		return securityAnswer1;
	}
	
	public void setSecurityAnswer1(String securityAnswer1) {
		this.securityAnswer1 = securityAnswer1;
	}
	
	public String getSecurityAnswer2() {
		return securityAnswer2;
	}
	
	public void setSecurityAnswer2(String securityAnswer2) {
		this.securityAnswer2 = securityAnswer2;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public Float getBalance() {
		return balance;
	}

	public void setBalance(float balance) {
		this.balance = balance;
	}

	public Boolean isNonUser() {
		return storeUser;
	}

	public void setNonUser(boolean storeUser) {
		this.storeUser = storeUser;
	}

	
	public boolean commitAccount(){
		try{
			DatabaseAccess accessDatabase = new DatabaseAccess();
			accessDatabase.addItemToDomain(this);
		}
		catch(Exception E)
		{
			return false;
		}
		return true;
	}
	
	
	public boolean removeFromSimpleDatabase(Account account){
		if(account.storeUser == false){
			return removeCustomerFromSimpleDatabase();
		}
		else{
			return removeStoreUserFromSimpleDatabase();
		}
	}

	private boolean removeStoreUserFromSimpleDatabase() {
		return false;
	}

	private boolean removeCustomerFromSimpleDatabase() {
		return false;
	}
	
	public static boolean checkUserNameAvailable(String email){
		return false;
	}
	
	public static boolean checkEmailFormat(String email){
		Boolean isValid = false;
		String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";  
		CharSequence inputStr = email;  
		Pattern pattern = Pattern.compile(expression,Pattern.CASE_INSENSITIVE);  
		Matcher matcher = pattern.matcher(inputStr);  
		if(matcher.matches()){  
			isValid = true;  
		}  
		return isValid;  
	}  
	
	public static Account[] checkUserNamePasswordAndGetAccount(String email, String hashPassword){
		Account account[] = null;
		DatabaseAccess accessDatabase = new DatabaseAccess();
		if(!checkEmailFormat(email)){
			return null;
		}
		account = accessDatabase.checkUserNamePasswordAndGetAccount(email, hashPassword);
		
		return account;
	}

	public String getCustomerID() {
		return customerID;
	}

	public void setCustomerID(String customerID) {
		this.customerID = customerID;
	}

	public void setAccountID(String accoundID) {
		this.accountID = accoundID;
	}

	public String getAccountID() {
		return accountID;
	}
	
}
